home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 2.7 KB | 62 lines | [TEXT/GEOL] |
- Item forwarded by PERRY.G to PHIL.TH
-
- Item 3697528 29-Oct-90 07:29PST
-
- From: V0683 Amoco Tech, Eric Berdahl,VAR
-
- To: MACAPP.TECH$ MacApp Technical
- GER.XSE0067 Germany - Norbert Lindenberg,IDV
-
- Sub: Re[2]: Auto-Initialized Objec
-
- Norbert,
-
- My argument against doing Failable things in Initialize methods (read:
- constructors) is two-fold, as you indicated.
-
- In the C++ world, constructors are wonderfull things. They can ensure that
- anytime after saying new TFoo, your instantiated object is internally
- self-consistent. This means that its pointers and references are real and that
- everything has a default value. However, the invocation of the constructor
- makes the lines of exception handling a little fuzzy, so we say "Don't do
- things in the constructor that can fail." One of these no-nos is necessarily
- memory allocation.
-
- Given the fact that the Initialize method is now the formalized constructor for
- MacApp objects, we can apply the same arugment to Robert's improvement to the
- MacApp MakeNewInstance routine. "But wait!" you say, "MacApp also provides a
- nice failure handling mechanism, so why can't I do failable things?" This is
- an excellent question because it gets to the root of the objective idea of
- constructors (read: Initialize method). Constructors (read: Initialize
- methods) construct. Initializers (read: IFoo methods) initialize.
- Constructors are sort of no-brainers for the idea of initialization. They put
- everything into "known, safe states." Eiffel has one such mechanism built into
- its whole being. Eiffel sets integers to 0, booleans to false, and object
- references to void. C++, because of such things as const and reference data
- members must have some formalized constructor to deal with these concepts, so
- it foists the initialization to the programmer. Pascal does nothing. MacApp
- leaves it to the first part of the IFoo method. Thus, IFoo can be thought of
- as the following:
-
- pascal void TFoo::IFoo()
- {
- // construct my Foo object with code here
- this->IFoosParentClass();
- WithFreeUponFailureDo { // WithFreeUponFailureDo courtesy of CPlusMacApp.h
- // initialize my Foo object with code here
- }
- }
-
- Objectively, this is messy because it combines the two concepts of construction
- and initialization into one method. A better object abstraction would be to
- separate these ideas into two separate methods. Thus, we come back to the
- concept that constructors construct and initializers initialize. Construction
- simply puts things into "safe, known states" and thus does not involve
- allocations or other failable things.
-
- Waxing eloquent (kind of),
- Eric Berdahl
- Amoco Technology Company
- AppleLink: V0683
-
-